<?xml version="1.0" encoding="UTF-8" ?>
<stylesheet version="1.0" xmlns="http://www.w3.org/1999/XSL/Transform" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>	

<xsl:template match="/">	
	
<!--/////////// GENERATED CODE FOR VISUAL 2005 ///////////////-->

Imports System
Imports System.Reflection
Imports System.Collections
Imports System.Collections.Generic
Imports USS = Evaluant.Uss
Imports Evaluant.Uss.ObjectContext

'///////////// Code for Visual 2005 VB.NET /////////////////////
	<xsl:apply-templates/>
	
</xsl:template>


<xsl:template match="Model">

Namespace <xsl:value-of select="@name"/>
	

	&lt;Serializable()&gt; _
	Public Class IPersistableGenericCollection(Of T As IPersistable)
		Inherits List(Of T)
	
		'/// &lt;summary&gt;
			'/// A reference to the Entity owning the collection. If it is null, the collection is autonomous.
			'///
		'///&lt;/summary&gt;
		Dim Protected _ParentEntity As Uss.Entity

		Dim Protected _Role As string

		'/// &lt;summary&gt;
		'///     &lt;para&gt;
		'///       Initializes a new instance of &lt;see cref='IPersistableCollection'/&gt;.
		'///		&lt;/para&gt;
		'/// &lt;/summary&gt;
		Public Sub New ()
		
			MyBase.New
		
		End Sub


		Public Sub New(ByVal parentEntity As Uss.Entity ,ByVal role As string ,ByVal data As IPersistable())
		
			_ParentEntity = parentEntity
			_Role = role

            For Each t As T In data
			    MyBase.Add(t)
			Next
			    
		End Sub

		'/// &lt;summary&gt;
		'///		&lt;para&gt;
		'///			Initializes a new instance of &lt;see cref='IPersistableGenericCollection'/&gt; containing any array of &lt;see cref='IPersistable'/&gt; objects.
		'///		&lt;/para&gt;
		'/// &lt;/summary&gt;
		'/// &lt;param name='value'&gt;
		'///		A array of &lt;see cref='T'/&gt; objects with which to intialize the collection
		'/// &lt;/param&gt;
		Public Sub New (ByVal val As T())
		
			Me.AddRange(val)
		
		End Sub
		'/// &lt;summary&gt;
		'///		&lt;para&gt;
		'///			Adds a &lt;see cref='T'/&gt; with the specified value to the
		'///		&lt;see cref='IPersistableGenericCollection'/&gt; .
		'/// &lt;/para&gt;
		'///
		'/// &lt;/summary&gt;
		'/// &lt;param name='value'&gt;
		'///		The &lt;see cref='T'/&gt; to add.
		'/// &lt;/param&gt;
		'/// &lt;returns&gt;
		'///		&lt;para&gt;The index at which the new element was inserted.&lt;/para&gt;
		'/// &lt;/returns&gt;
		'/// &lt;seealso cref=' IPersistableGenericCollection.AddRange'/&gt;
		Public Shadows Sub Add(val As T )
		
			'// if the collection is not autonomous, add the added entity to the parent's role
			If Not(_ParentEntity Is Nothing) Then
				_ParentEntity.AddValue(_Role, val.Entity)
			
			End If	
			 MyBase.Add(val)
			 
		End Sub

		Public Shadows Sub Remove(ByVal val As T )
		
			If Not(_ParentEntity Is Nothing) Then
				_ParentEntity.RemoveReference(val.Entity)
			End If
			MyBase.Remove(val)
		
		End Sub

	End Class
				<!--///////////////ENUMERATION DECLARATION ////////////////-->
	<xsl:apply-templates select="Enumeration" mode="enum"/>
		
	<xsl:apply-templates>
		<xsl:with-param name="namespace" select="@name"/>
	</xsl:apply-templates>
	
End Namespace
</xsl:template>	


<!--///////////////ENUMERATION DECLARATION (Suite) ////////////////-->
<xsl:template match="Enumeration" mode="enum">
	Public Enum <![CDATA[ ]]> <xsl:value-of select="@name"/><![CDATA[
	 ]]>
	<xsl:apply-templates select="Literal" mode="enum"/>
	End Enum
</xsl:template>

<xsl:template match="Literal" mode="enum">
	<xsl:value-of select="@name"/><![CDATA[ 
	]]>
</xsl:template>


<!--///////////////INTERFACE DECLARATION///////////////////-->


<xsl:template match="Interface">

	Public Interface <xsl:value-of select="@name"/>
	
	<xsl:apply-templates select="Property" mode="Interface">
	</xsl:apply-templates>	
	End Interface
</xsl:template>


<xsl:template match="Property" mode="Interface">
	Property <![CDATA[ ]]><xsl:value-of select="@name"/>  As <![CDATA[ ]]><xsl:value-of select="@type"/>
</xsl:template>


<!--/////////////// MODELE METIER ///////////////////////////-->

<xsl:template match="Class">
<xsl:param name="namespace"/>

	Public Class <xsl:value-of select="@name"/>Collection_
		Inherits IPersistableGenericCollection(Of <xsl:value-of select="@name"/>)
	End Class

	&lt;Serializable()&gt; _
	<xsl:choose>
		<xsl:when test="@inherit" >
			<xsl:choose>
				<xsl:when test="@implement">Public Partial Class <xsl:value-of select="@name"/>
					Inherits <xsl:value-of select="@inherit"/>
					Implements <xsl:value-of select="@implement"/>
				</xsl:when>
				<xsl:otherwise>Public Partial Class <xsl:value-of select="@name"/>
					Inherits <xsl:value-of select="@inherit"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:when>
		<xsl:otherwise>
			<xsl:choose>
				<xsl:when test="@implement">public partial class <xsl:value-of select="@name"/>
					Implements IPersistable,  <xsl:value-of select="@implement"/>
				</xsl:when>
				<xsl:otherwise>public partial class <xsl:value-of select="@name"/>
					Implements IPersistable	
				</xsl:otherwise>
			</xsl:choose>
		</xsl:otherwise>
	</xsl:choose>

	
	<xsl:if test = "not(@inherit)">	
		Dim Protected _Entity As Uss.Entity
		
		&lt;NonSerialized()&gt; _
		Dim Protected _PersistenceManager As ObjectContext
		
		Property Entity() As Uss.Entity _
			Implements IPersistable.Entity 
		
			Get
			
				Return _Entity
				
			End Get
			
			Set
			
				_Entity = value
				
			End Set
			
		End Property
		
		Property  ObjectContext() As ObjectContext _
				Implements IPersistable.ObjectContext
		
			Get
			
				Return _PersistenceManager
				
			End Get
			
			Set
			
				_PersistenceManager = value
				
			End Set
		
		End Property	

		
		Property Id() As String _
			Implements IPersistable.Id
		
			Get
			
				Return _Entity.Id
				
			End Get
			
			Set
			
				_Entity.Id = value
				
			End Set
			
		End Property
			
		Protected Overrides Sub Finalize()
			_PersistenceManager.RemoveFromCache(Me)
		End Sub

	</xsl:if>
	
	<xsl:if test="not(@inherit)">
	<xsl:text>
		Sub TrackChildren() _
			 Implements IPersistable.TrackChildren
		
			If _PersistenceManager Is Nothing Then
				Return
			End If
			
	</xsl:text>
	</xsl:if>
	
	<xsl:if test="(@inherit)">
	<xsl:text>
		Shadows Sub TrackChildren()
		
			If _PersistenceManager Is Nothing Then
				Return
			End If
			
	</xsl:text>
	</xsl:if>
		
	<xsl:if test="not(@inherit)">
	
		<xsl:variable name="className" select="@name"/>
		<xsl:apply-templates select="//Relationship" mode="TrackChildren">
			<xsl:with-param name="className" select="$className"/>
		</xsl:apply-templates>
		End Sub
		
		<xsl:text>
		Sub DeleteChildren() _
			Implements IPersistable.DeleteChildren
		
		</xsl:text>
	</xsl:if> 
	<xsl:if test="(@inherit)">
		<xsl:variable name="className" select="@name"/>
		<xsl:apply-templates select="//Relationship" mode="TrackChildren">
			<xsl:with-param name="className" select="$className"/>
		</xsl:apply-templates>
		
		<xsl:variable name="className2" select="@inherit"/>
		<xsl:apply-templates select="//Relationship" mode="TrackChildren">
			<xsl:with-param name="className" select="$className2"/>
		</xsl:apply-templates> 
		End Sub
		
		<xsl:text>
		Shadows Sub DeleteChildren()
			
		</xsl:text>
	</xsl:if>
		
	
	
	<xsl:if test="not(@inherit)">
			<xsl:variable name="className" select="@name"/>
			<xsl:apply-templates select="//Relationship" mode="DeleteChildren">
				<xsl:with-param name="className" select="$className"/>
			</xsl:apply-templates>
		End Sub
	</xsl:if> 
	<xsl:if test="(@inherit)">
		<xsl:variable name="className" select="@name"/>
			<xsl:apply-templates select="//Relationship" mode="DeleteChildren">
				<xsl:with-param name="className" select="$className"/>
			</xsl:apply-templates>
			
			<xsl:variable name="className2" select="@inherit"/>
			<xsl:apply-templates select="//Relationship" mode="DeleteChildren">
				<xsl:with-param name="className" select="$className2"/>
			</xsl:apply-templates>
		End Sub
	</xsl:if>
	
<!--///////////////////// INTERFACE ////////////////-->

	<apply-templates select="//Interface" mode="suite">
		<xsl:with-param name="implement" select="@implement"/>
		<xsl:with-param name="className" select="@name"/>
	</apply-templates>
<!--//////////////////////////////////////////////////-->

		Public Sub New()
		
			MyBase.New
			Dim fullType As string 
			fullType= ObjectContext.ConvertNamespaceDomainToEuss(Me.GetType().FullName)
			Me._Entity = new Uss.Entity(fullType)
			Dim _Parents As ParentRelationships = new ParentRelationships(Me)
		
		End Sub

		Public Sub New(e As Uss.Entity)
		
			Me.New
			Me._Entity = e
		
		End Sub
		
		Private _Parents As ParentRelationships
		<xsl:if test="(@inherit)">
		Public Shadows ReadOnly Property Parents() As ParentRelationships
		
			Get 
			
				Return _Parents
				
			End Get
		
		End Property
		
		Public Shadows Class ParentRelationships
	</xsl:if>
	<xsl:if test="not(@inherit)">
		Public ReadOnly Property Parents() As ParentRelationships
		
			Get 
			
				Return _Parents
				
			End Get
		
		End Property
		
		Public Class ParentRelationships
	</xsl:if>
		
			Dim Private _Owner As IPersistable

			Public Sub New(o As IPersistable)
			
				_Owner = o
				
			End Sub
		
			<xsl:variable name="parent" select= "true()"/>
		
			<xsl:apply-templates select="." mode="relation">
				<xsl:with-param name="parent" select="$parent"/>
				<xsl:with-param name="namespace" select="$namespace"/>
			</xsl:apply-templates>
			
		End Class
		
		<xsl:apply-templates select="." mode="relation">
			<xsl:with-param name="parent" select= "false()"/>
			<xsl:with-param name="namespace" select="$namespace"/>
		</xsl:apply-templates>
		
		<xsl:apply-templates select="/" mode="enumeration">
			<xsl:with-param name="className" select="@name"/>
			<xsl:with-param name="namespace" select="$namespace"/>
		</xsl:apply-templates>
		
	End Class
</xsl:template>
	
		
<!--///////////////////// INTERFACE PROPERTIES ////////////////-->

<xsl:template match="//Interface" mode="suite">
<xsl:param name="implement"/>
	<xsl:if test = "@name = $implement">
		<xsl:apply-templates select="Property" mode="InterfaceCode">
			<xsl:with-param name="interfaceName" select="@name"/>
		</xsl:apply-templates>
	</xsl:if>
</xsl:template>


<xsl:template match="Property" mode="InterfaceCode">
	<xsl:param name="interfaceName"/>
								
	Public Property <xsl:value-of select="@name"/> As <xsl:value-of select="@type"/> _
			Implements <xsl:value-of select="$interfaceName"/>.<xsl:value-of select="@name"/>
		
			Get 
			
				Return CType(Me._Entity("<xsl:value-of select="@name"/>"),<xsl:value-of select="@type"/>)
			
			End Get
			
			Set 
			
				Me._Entity("<xsl:value-of select="@name"/>") = value
				
			End Set
			
		End Property
</xsl:template>



<!--/////////////// ENUMERATION ///////////////////////////-->
		
<xsl:template match="/" mode="enumeration">
	<xsl:param name="className"/>
	
	<xsl:apply-templates select="//Relationship" mode="enumeration">
		<xsl:with-param name ="className" select="$className"/>
	</xsl:apply-templates>
	
</xsl:template>


<xsl:template match="//Relationship" mode="enumeration">
<xsl:param name="className"/>

		<xsl:variable name="childname" select="Child/@name" />
		<xsl:variable name="parentname" select="Parent/@name" />
		<xsl:variable name="childrole" select="Child/@role" />
						
		
		<xsl:apply-templates select="//Enumeration" mode="enumeration">				
			<xsl:with-param name ="childname" select="$childname"/>
			<xsl:with-param name ="parentname" select="$parentname"/>
			<xsl:with-param name ="childrole" select="$childrole"/>
			<xsl:with-param name ="className" select="$className"/>
		</xsl:apply-templates>		
				
</xsl:template>

<xsl:template match="//Enumeration" mode="enumeration">
<xsl:param name="childname"/>
<xsl:param name="parentname"/>
<xsl:param name="childrole"/>
<xsl:param name="className"/>

		<xsl:if test="$childname = @name">
				
			<xsl:if test="$className = $parentname">	
		Public Property <xsl:value-of select="$childname"/>() As <xsl:value-of select="$childrole"/>
		
			Get 
			
				Return CType(Me._Entity("<xsl:value-of select="$childrole"/>"), <xsl:value-of select="$childname"/>)
				
			End Get
			
		
			Set 
			
				Me._Entity("<xsl:value-of select="$childrole"/>") = value
				
			End Set
			
		End Property
			</xsl:if>
		</xsl:if>
</xsl:template>

<!--/////////////////TRACK AND DELETE CHILDREN////////////////////////-->	
	
<xsl:template match="//Relationship" mode="DeleteChildren">
	<xsl:param name="className"/>
	<xsl:param name="namespace"/>
	
	<xsl:if test="@type ='composition'" >
		<xsl:apply-templates select="." mode="CompoDelete">
			<xsl:with-param name="className" select="$className"/>
			<xsl:with-param name="namespace" select="$namespace"/> 
		</xsl:apply-templates>	
	</xsl:if>
</xsl:template>

<xsl:template match="//Relationship" mode="TrackChildren">
<xsl:param name="className"/>
	<xsl:if test = "($className = Parent/@name) and ((Child/@multiplicity = '*') or (Child/@multiplicity = '0..*'))">
	
			If Not(_<xsl:value-of select="Child/@role"/> Is Nothing)
				_PersistenceManager.TrackObject((_<xsl:value-of select="Child/@role"/>).ToArray())
			End If			
	</xsl:if>

</xsl:template>
			

<!--/////////////////COMPOSITION DELETE////////////////////////-->	
<xsl:template match="Relationship" mode="CompoDelete">
	<xsl:param name="className"/>
	<xsl:param name="namespace"/>
	
		<xsl:variable name="childMultiplicity" select="Child/@multiplicity"/>
			<xsl:variable name="parentMultiplicity" select="Parent/@multiplicity"/>
			
				<xsl:choose>
					<xsl:when test= "(($childMultiplicity = 1) or ($childMultiplicity ='0..1') or ($childMultiplicity ='1:1')) and (($parentMultiplicity = 1) or ($parentMultiplicity = '0..1') or ($parentMultiplicity = '1:1'))">
						
						<xsl:apply-templates select="." mode="OneToOneDelete">
							<xsl:with-param name="className" select="$className"/> 
							<xsl:with-param name="namespace" select="$namespace"/> 
						</xsl:apply-templates>
						
					</xsl:when>
					<xsl:when test="(($childMultiplicity='*')or ($childMultiplicity ='0..*')) and (($parentMultiplicity = 1) or ($parentMultiplicity = '0..1') or ($parentMultiplicity = '1:1'))">
						
						<xsl:apply-templates select="." mode="OneToManyDelete">
							<xsl:with-param name="className" select="$className"/> 
							<xsl:with-param name="namespace" select="$namespace"/> 
						</xsl:apply-templates>
					</xsl:when>
				</xsl:choose>
</xsl:template>	

<xsl:template match="Relationship" mode="OneToOneDelete">
			
	<xsl:param name="className"/>				

	<xsl:variable name="name" select="Child/@name" />	
	
	<xsl:if test="$name = $className">
			_PersistenceManager.Delete(<xsl:value-of select="Parent/@role"/>.ToArray())
	</xsl:if>
</xsl:template>

<xsl:template match="Relationship" mode="OneToManyDelete">

	<xsl:param name="className"/>

	<xsl:variable name="name" select="Parent/@name" />

	<xsl:if test="$name = $className">
			_PersistenceManager.Delete(<xsl:value-of select="Child/@role"/>.ToArray())
	</xsl:if>
</xsl:template>

	
	
<!--/////////////// PROPRIETES ///////////////////////////-->

<xsl:template match="Property">
	
		Public Property <xsl:value-of select="@name"/> As <xsl:value-of select="@type"/>
		
			Get 
			 
				Return CType(Me._Entity("<xsl:value-of select="@name"/>"),<xsl:value-of select="@type"/>)
			End Get
			
		
			Set 
			
				Me._Entity("<xsl:value-of select="@name"/>") = value 
				
			End Set
			
		End Property
		<xsl:apply-templates/>	
	</xsl:template>

<!--/////////////// RELATIONS  ///////////////////////////-->

	<xsl:template match="Class" mode="relation">
	<xsl:param name="parent"/>
	<xsl:param name="namespace"/>
	
		<xsl:variable name="className" select="@name"/>
					
			<xsl:apply-templates select="//Relationship" mode="relation">
				<xsl:with-param name="className" select="$className"/>
				<xsl:with-param name="parent" select="$parent"/>
				<xsl:with-param name="namespace" select="$namespace"/>
			</xsl:apply-templates>
		
		<xsl:if test="$parent = false()">
			<xsl:apply-templates select="Property"/>
			<xsl:apply-templates select="/" mode="enumeration">
				<xsl:with-param name="namespace" select="$namespace"/>
			</xsl:apply-templates>
			
		</xsl:if>
		
	</xsl:template>
	
	<xsl:template match="Relationship" mode="relation">
	<xsl:param name="className"/>
	<xsl:param name="parent"/>
	<xsl:param name="namespace"/>
	
		<xsl:variable name="typeRelation" select="@type" />
			
			<xsl:choose>
				<xsl:when test="$typeRelation='composition'" >
					
					<xsl:apply-templates select="." mode="Composition">
						<xsl:with-param name="className" select="$className"/>
						<xsl:with-param name="parent" select="$parent"/>
						<xsl:with-param name="namespace" select="$namespace"/>
					</xsl:apply-templates>
							
				</xsl:when>
				<xsl:when test="$typeRelation= 'aggregation'" >
					<xsl:apply-templates select="." mode="Aggregation" >
						<xsl:with-param name="className" select="$className"/> 
						<xsl:with-param name="parent" select="$parent"/>
						<xsl:with-param name="namespace" select="$namespace"/>
					</xsl:apply-templates>
						
				</xsl:when>
				<xsl:otherwise>
					
				</xsl:otherwise>
			</xsl:choose>
	</xsl:template>
	
<!--/////////////// COMPOSITION ///////////////////////////-->		
		
		<xsl:template match="Relationship" mode="Composition">	
		<xsl:param name="className"/>
		<xsl:param name="parent"/>
		<xsl:param name="namespace"/>
		
			
			<xsl:variable name="childMultiplicity" select="Child/@multiplicity"/>
			<xsl:variable name="parentMultiplicity" select="Parent/@multiplicity"/>
			
				<xsl:choose>
					<xsl:when test="($childMultiplicity = 1) or ($childMultiplicity ='0..1') or ($childMultiplicity ='1:1')">
						
						<xsl:apply-templates select="." mode="CRelationOneToOne">
							<xsl:with-param name="className" select="$className"/> 
							<xsl:with-param name="parent" select="$parent"/>
							<xsl:with-param name="namespace" select="$namespace"/>
						</xsl:apply-templates>
						
					</xsl:when>
					<xsl:when test="($childMultiplicity='*')or ($childMultiplicity ='0..*')">
						
						<xsl:apply-templates select="." mode="CRelationOneToMany">
							<xsl:with-param name="className" select="$className"/> 
							<xsl:with-param name="parent" select="$parent"/>
							<xsl:with-param name="namespace" select="$namespace"/>
						</xsl:apply-templates>
					</xsl:when>
				</xsl:choose>
					
		<xsl:apply-templates/>	
	</xsl:template>	


<!--/////////////// COMPOSITION ONE TO ONE ///////////////////////////-->	
	
<xsl:template match="Relationship" mode="CRelationOneToOne">	
<xsl:param name="className"/>
<xsl:param name="parent"/>
<xsl:param name="namespace"/>
	
	<xsl:variable name="parentName" select="Parent/@name"/>	
	<xsl:variable name="childName" select="Child/@name"/>
	<xsl:variable name="childRole" select="Child/@role"/>
	<xsl:variable name="parentRole" select="Parent/@role"/>
		
	
	<xsl:if test="($parentName = $className) and (//Enumeration/@name != childName) and ($parent = false())">
						
		'///Composition OneToOne/////	
				
			Dim Protected _<xsl:value-of select="$childRole"/> As <xsl:value-of select="$childName"/>
		
			Public Property <xsl:value-of select="$childName"/>() <![CDATA[ ]]><xsl:value-of select="$childName"/>
			
				Get 
				
					If _<xsl:value-of select="$childRole"/> Is Nothing) Then
					
						_<xsl:value-of select="$childRole"/> = new <xsl:value-of select="$childName"/>(_Entity.GetEntity("<xsl:value-of select="$childName"/>"))
						
					
					Return _<xsl:value-of select="$childRole"/>
				
				End Get
				
				Set
				
					If value Is Nothing Then
					
						_Entity.RemoveReference("<xsl:value-of select="$childName"/>")
					
					Else
					
						If Not(_Entity.GetEntity("<xsl:value-of select="$childName"/>") Is Nothing) Then
							_Entity.RemoveReference("<xsl:value-of select="$childName"/>")
						End If

						_Entity.AddValue("<xsl:value-of select="$childName"/>", ((IPersistable)value).Entity)
					
					End If
					
					_<xsl:value-of select="$childRole"/> = value
					
				End Set
				
			End Property
	</xsl:if>
	
	<xsl:if test = "$parent = true()">
		<xsl:if test="($childName = $className)">
	
		Public Function Get<xsl:value-of select="$parentRole"/>() AS <xsl:value-of select="$parentName"/> 
		
			Dim persistables As IPersistable()  = _Owner.ObjectContext.Load(String.Format("<xsl:value-of select="$namespace"/>:<xsl:value-of select="$parentName"/>[<xsl:value-of select="$childRole"/>.Id = '{0}']", _Owner.Id))
			
			If (persistables.Length>0) Then
			
			
					Return CType(persistables(0),<xsl:value-of select="$parentName"/>)
			Else
			
					Return Nothing 
			
			End If
			
		End Function
	
		</xsl:if>
	</xsl:if>
	
	<xsl:apply-templates/>			
		
	</xsl:template>
	
	

<!--/////////////// COMPOSITION ONE TO MANY ///////////////////////////-->	
	
	<xsl:template match="Relationship" mode="CRelationOneToMany">	
	<xsl:param name="className"/>
	<xsl:param name="parent"/>
	<xsl:param name="namespace"/>

	<xsl:variable name="parentName" select="Parent/@name"/>	
	<xsl:variable name="childName" select="Child/@name"/>
	<xsl:variable name="childRole" select="Child/@role"/>
	<xsl:variable name="parentRole" select="Parent/@role"/>
		
	
	<xsl:if test="($parentName = $className) and ($parent = false())">

		'///Composition OneToMany/////

		Protected _<xsl:value-of select="$childRole"/> As IPersistableGenericCollection(Of <xsl:value-of select="$childName"/>)
		Public ReadOnly Property <xsl:value-of select="$childRole"/>() As IPersistableGenericCollection(Of <xsl:value-of select="$childName"/>)
		
			Get
			
				If _<xsl:value-of select="$childRole"/> Is Nothing Then
					
					If _PersistenceManager Is Nothing Then
						Dim data() As <xsl:value-of select="$childName"/> = {}
						_<xsl:value-of select="$childRole"/> = new IPersistableGenericCollection(Of <xsl:value-of select="$childName"/>)(_Entity, "<xsl:value-of select="$childRole"/>", data)
					Else
					

						Dim refs As IPersistable() = _PersistenceManager.LoadReference(_Entity, "<xsl:value-of select="$childRole"/>")
						_<xsl:value-of select="$childRole"/> = new IPersistableGenericCollection(Of <xsl:value-of select="$childName"/>)(_Entity, "<xsl:value-of select="$childRole"/>", refs)
						
					End If
					
				End If
			Return _<xsl:value-of select="$childRole"/>
			
			End Get
			
		End Property

	</xsl:if>
	
	<xsl:if test = "$parent = true()">
		<xsl:if test="($childName = $className)">
	
		Public Function Get<xsl:value-of select="$parentRole"/>() AS <xsl:value-of select="$parentName"/> 
		
			Dim persistables As IPersistable()  = _Owner.ObjectContext.Load(String.Format("<xsl:value-of select="$namespace"/>:<xsl:value-of select="$parentName"/>[<xsl:value-of select="$childRole"/>.Id = '{0}']", _Owner.Id))
			
			If (persistables.Length>0) Then
			
			
					Return CType(persistables(0),<xsl:value-of select="$parentName"/>)
			Else
			
					Return Nothing 
			
			End If
			
		End Function
	
		</xsl:if>
	</xsl:if>
	
	<xsl:apply-templates/>			
		
	</xsl:template>
	


<!--/////////////// AGGREGATION///////////////////////////-->	
		<xsl:template match="Relationship" mode="Aggregation">		
		<xsl:param name="className"/>
		<xsl:param name="parent"/>
		<xsl:param name="namespace"/>
			
			
			<xsl:variable name="childMultiplicity" select="Child/@multiplicity"/>
			<xsl:variable name="parentMultiplicity" select="Parent/@multiplicity"/>
			
				<xsl:choose>
					<xsl:when test="($childMultiplicity = 1) or ($childMultiplicity ='0..1') or ($childMultiplicity ='1:1')">
						
						<xsl:apply-templates select="." mode="ARelationOneToOne">
							<xsl:with-param name="className" select="$className"/>
							<xsl:with-param name="parent" select="$parent"/>
							<xsl:with-param name="parentMultiplicity" select="$parentMultiplicity"/>
							<xsl:with-param name="namespace" select="$namespace"/>
						</xsl:apply-templates>
						
					</xsl:when>
					<xsl:when test="($childMultiplicity='*')or ($childMultiplicity ='0..*')">
						
						<xsl:apply-templates select="." mode="ARelationOneToMany">
							<xsl:with-param name="className" select="$className"/>
							<xsl:with-param name="parent" select="$parent"/>
							<xsl:with-param name="parentMultiplicity" select="$parentMultiplicity"/>
							<xsl:with-param name="namespace" select="$namespace"/>
						</xsl:apply-templates>
					</xsl:when>
			
					<xsl:otherwise>
					
					</xsl:otherwise>
				</xsl:choose>
					
		<xsl:apply-templates/>	
	</xsl:template>	


<!--/////////////// AGGREGATION ONE TO ONE ///////////////////////////-->	
	
	<xsl:template match="Relationship" mode="ARelationOneToOne">	
	<xsl:param name="className"/>			
	<xsl:param name="parentMultiplicity"/>
	<xsl:param name="parent"/>
	<xsl:param name="namespace"/>

	<xsl:variable name="parentName" select="Parent/@name" />
	<xsl:variable name="parentRole" select="Parent/@role" />
	<xsl:variable name="childName" select="Child/@name" />	
	<xsl:variable name="childRole" select="Child/@role" />	
		
	
	<xsl:if test="($parentName = $className) and ($parent = false())">
						
	'	///Aggregation OneToOne/////	
				
			Protected  _<xsl:value-of select="$childRole"/> As <xsl:value-of select="$childName"/>
		
			Public Property <xsl:value-of select="$childName"/>() As <xsl:value-of select="$childName"/>
			
				Get 
				
					If _<xsl:value-of select="$childRole"/>Is Nothing Then
					
						_<xsl:value-of select="$childRole"/> = new <xsl:value-of select="$childName"/>(_Entity.GetEntity("<xsl:value-of select="$childName"/>"))		
					
					Return _<xsl:value-of select="$childRole"/>
				
				End Get
				
				Set
				
					If(value == null)
					
						_Entity.RemoveReference("<xsl:value-of select="$childName"/>")
					
					Else
					
						If Not(_Entity.GetEntity("<xsl:value-of select="$childName"/>") Is Nothing) Then
							_Entity.RemoveReference("<xsl:value-of select="$childName"/>")
						_Entity.AddValue("<xsl:value-of select="$childName"/>", ((IPersistable)value).Entity)
					
					End If
					_<xsl:value-of select="$childRole"/> = value
				
				End Set
				
			End Property

	</xsl:if>
	
	
	<xsl:if test = "$parent = true()">
		<xsl:if test="(Child/@name = $className) and (($parentMultiplicity = '*') or ($parentMultiplicity = '0..*'))">
						
		Public Function Get<xsl:value-of select="$parentRole"/>() As <xsl:value-of select="$parentName"/>()
		
			Dim persistables As IPersistable() = _Owner.ObjectContext.Load(String.Format("<xsl:value-of select="$namespace"/>:<xsl:value-of select="$parentName"/>[<xsl:value-of select="$childRole"/>.Id = '{0}']", _Owner.Id))
			''Dim result As <xsl:value-of select="$parentName"/>()  = new <xsl:value-of select="$parentName"/>(persistables.Length)
			Dim result(persistables.Length) As <xsl:value-of select="$parentName"/>

			persistables.CopyTo(result, 0)

			return result
			
		End Function
		
		</xsl:if>
	
		<xsl:if test="($childName = $className) and (($parentMultiplicity = 1) or ($parentMultiplicity = '0..1') or ($parentMultiplicity = '1:1'))">
	
		Public Function Get<xsl:value-of select="$parentRole"/>() As <xsl:value-of select="$parentName"/> 
		
		Dim persistables As IPersistable()  = _Owner.ObjectContext.Load(String.Format("<xsl:value-of select="$namespace"/>:<xsl:value-of select="$parentName"/>[<xsl:value-of select="$childRole"/>.Id = '{0}']", _Owner.Id))
			
			If (persistables.Length>0) Then
			
			
					Return CType(persistables(0),<xsl:value-of select="$parentName"/>)
			Else
			
					Return Nothing 
			
			End If
			
		End Function
	
		</xsl:if>
	</xsl:if>
	
	<xsl:apply-templates/>			
		
	</xsl:template>
	
	
	
<!--/////////////// AGGREGATION ONE TO MANY ///////////////////////////-->	
	
	<xsl:template match="Relationship" mode="ARelationOneToMany">		
	<xsl:param name="className"/>
	<xsl:param name="parent"/>				
	<xsl:param name="parentMultiplicity"/>
	<xsl:param name="namespace"/>			

	<xsl:variable name="parentName" select="Parent/@name" />
	<xsl:variable name="parentRole" select="Parent/@role" />
	<xsl:variable name="childName" select="Child/@name" />	
	<xsl:variable name="childRole" select="Child/@role" />	
		
	
	<xsl:if test="($parentName = $className) and ($parent = false())">
						
		Protected _<xsl:value-of select="$childRole"/> As IPersistableGenericCollection(Of <xsl:value-of select="$childName"/>)

		Public ReadOnly Property <xsl:value-of select="$childRole"/>() As IPersistableGenericCollection(Of <xsl:value-of select="$childName"/>)
		
			Get
			
				If _<xsl:value-of select="$childRole"/> Is Nothing Then
				
					If _PersistenceManager Is Nothing
						Dim data() As <xsl:value-of select="$childName"/> = {}
						_<xsl:value-of select="$childRole"/> = new IPersistableGenericCollection(Of <xsl:value-of select="$childName"/>)(_Entity, "<xsl:value-of select="$childRole"/>", data)
					Else
					

						Dim refs As IPersistable() = _PersistenceManager.LoadReference(_Entity, "<xsl:value-of select="$childRole"/>")
						_<xsl:value-of select="$childRole"/> = new IPersistableGenericCollection (Of <xsl:value-of select="$childName"/>)(_Entity, "<xsl:value-of select="$childRole"/>", refs)
					
					End If
				
				End If
				
				Return _<xsl:value-of select="$childRole"/>
			
			End Get
			
		End Property
			
	</xsl:if>
	
	<xsl:if test = "$parent = true()">
		<xsl:if test="(Child/@name = $className) and (($parentMultiplicity = '*') or ($parentMultiplicity = '0..*'))">
						
		Public Function Get<xsl:value-of select="$parentRole"/>() As <xsl:value-of select="$parentName"/>()
		
			Dim persistables As IPersistable() = _Owner.ObjectContext.Load(String.Format("<xsl:value-of select="$namespace"/>:<xsl:value-of select="$parentName"/>[<xsl:value-of select="$childRole"/>.Id = '{0}']", _Owner.Id))
			''Dim result As <xsl:value-of select="$parentName"/>()  = new <xsl:value-of select="$parentName"/>(persistables.Length)
			Dim result(persistables.Length) As <xsl:value-of select="$parentName"/>
			
			persistables.CopyTo(result, 0)

			return result
		End Function
	
		</xsl:if>
	
		<xsl:if test="($childName = $className) and (($parentMultiplicity = 1) or ($parentMultiplicity = '0..1') or ($parentMultiplicity = '1:1'))">
	
		Public Function Get<xsl:value-of select="$parentRole"/>() As <xsl:value-of select="$parentName"/> 
		
		Dim persistables As IPersistable()  = _Owner.ObjectContext.Load(String.Format("<xsl:value-of select="$namespace"/>:<xsl:value-of select="$parentName"/>[<xsl:value-of select="$childRole"/>.Id = '{0}']", _Owner.Id))
			
			If (persistables.Length>0) Then
			
			
					Return CType(persistables(0),<xsl:value-of select="$parentName"/>)
			Else
			
					Return Nothing 
			
			End If
			
		End Function
	
		</xsl:if>
	</xsl:if>
	
	<xsl:apply-templates/>			
		
	</xsl:template>




</stylesheet>
